package utils 
{
	import flash.geom.Point;
	/**
	 * ...
	 * @author Dmitriy Gaiduk
	 */
	public class MathUtil 
	{
		
		public function MathUtil() 
		{
			
		}
		
		public static function triangleAreaSign(a:Point, b:Point, c:Point):Number
		{
			return (b.x - a.x) * (c.y - a.y) - (b.y - a.y) * (c.x - a.x);
		}
		
		/**
		 * Check intersect segments AB and CD in line
		 * @return
		 */
		public static function intersectSegmetsInLine(a:Number, b:Number, c:Number, d:Number):Boolean
		{
			var A:Number = a;
			var B:Number = b;
			var C:Number = c;
			var D:Number = d;
			if (A > B)
			{
				//swap
				A = b;
				B = a;
			}
			if (C > D)
			{
				// swap
				C = d;
				D = c;
			}
			return Math.max(A, C) <= Math.min(B, D);
		}
		
		public static function intersectBoundingBox(a:Point, b:Point, c:Point, d:Point):Boolean
		{
				return intersectSegmetsInLine (a.x, b.x, c.x, d.x)
					&& intersectSegmetsInLine (a.y, b.y, c.y, d.y);
		}
		
		public static function intersectSegments(a:Point, b:Point, c:Point, d:Point):Boolean
		{
			return intersectSegmetsInLine (a.x, b.x, c.x, d.x)
					&& intersectSegmetsInLine (a.y, b.y, c.y, d.y)
					&& triangleAreaSign(a,b,c) * triangleAreaSign(a,b,d) <= 0
					&& triangleAreaSign(c,d,a) * triangleAreaSign(c,d,b) <= 0;
		}
	}

}